package com.chris.homecare.data
{
	import com.chris.homecare.utils.CommonUtils;
	import com.chris.homecare.utils.FileDownloader;
	
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	
	import feathers.data.ListCollection;
	
	public class FetchRemoteData
	{
		private static var singletonInstance:FetchRemoteData;
		
		public static var backPage:String = "MENU_SCREEN";
		
		public static var choosenReviewData:ReviewData = null;
		public static var choosenEmployeeData:Employee = null;
		public static var choosenJobData:Job = null;
		public static var choosenServiceData:Services = null;
		public static var choosenUpdateId:String = "";
		public static var choosenPatient:Object = {header:"Click to select", address:""};
		
		public static var currentRatingListCollection:ListCollection;
		
		public function FetchRemoteData()
		{
			if(singletonInstance) throw new Error("Client already instantiated");
			
			else singletonInstance = this;
		}
		
		public static function getClient():FetchRemoteData
		{
			if(!singletonInstance) singletonInstance = new FetchRemoteData();
			
			return singletonInstance;
		}
		
		public function getBusinessPageData():void
		{
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=info&method=getPageInfo&pagename=2&format=json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onBusinessDataReceived);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		public function populateJobPosition():void
		{
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=getSiteRoles&format=json";
			//request.url="http://172.22.0.104/test/jobRoles.json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onPositionListDataRecieved);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		public function pouplateTeamListData():void
		{
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=getUsers&format=json";
			//request.url="http://172.22.0.104/test/team.json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onTeamListDataRecieved);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		public function pouplateUpdatesListData():void
		{
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=getUpdatesList&format=json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onUpdatesListDataRecieved);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		public function populateJobListData():void
		{
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=getJobListInfo&format=json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onJobsListDataRecieved);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		public function populateReviewData():void
		{
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=getAllReviewInformations&format=json";
			//request.url="http://172.22.0.104/test/reviews.json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onReviewDataReceived);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		
		protected function onReviewDataReceived(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			var reviewArray:Array = jsonArray.sitter.response.getReviewList;
			DataUtil.insertReviewData(reviewArray);
		}
		protected function onJobsListDataRecieved(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			var jobsArray:Array = jsonArray.sitter.response.getJobsList;
			DataUtil.insertJobPostingsData(jobsArray);
		}
		
		protected function onUpdatesListDataRecieved(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			
			var updatesArray:Array = new Array();
			var updatesJson:Array = jsonArray.sitter.response.getUpdatesList;
			DataUtil.checkAndInsertUpdates(updatesJson);
		}		
		
		public function populateEvents():void
		{
			populateEventsCategory();
			populateEventsData();
		}
		
		private function populateEventsCategory():void
		{
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=getEventsCategory&format=json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onEventsCategoryRecieved);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		private function populateEventsData():void
		{
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=getEventsList&format=json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onEventsDataRecieved);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		public function populateOurServices():void
		{
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=getOurServicesInfo&format=json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onServiceDataRecieved);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		public function getPatientList(userEmail:String):void
		{
			if(userEmail == "") return;
			
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=getPatientInforByStaffEmail&staff_email="+userEmail+"&format=json";
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onPatientListRecieved);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		protected function onPatientListRecieved(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			var patientListArray:Array = new Array();
			var remotePatListArray:Array = jsonArray.sitter.response.getReviewList;
			
			for each(var patientObj:Object in remotePatListArray)
			{
				var patObj:Object = new Object();
				patObj.header = patientObj.p_name;
				patObj.address = patientObj.p_address;
				patientListArray.push(patObj);
			}
			
			CommonUtils.patientListCollection = new ListCollection(patientListArray);
		}
		
		protected function onServiceDataRecieved(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			var serviceObjectArray:Array = new Array();
			var remoteServiceArray:Array = jsonArray.sitter.response.getServiceList;
			
			for each(var jobTitleObj:Object in remoteServiceArray)
			{
				var serviceObject:Object = new Object();
				serviceObject.id = jobTitleObj.cat_id;
				serviceObject.heading = jobTitleObj.cat_name;
				serviceObject.content = jobTitleObj.cat_description;
				serviceObjectArray.push(serviceObject);
			}
			DataUtil.insertOurServices(serviceObjectArray);
		}
		
		protected function onEventsCategoryRecieved(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			var evtsCategoryArray:Array = new Array();
			var remoteEvtCatArray:Array = jsonArray.sitter.response.eventCategory;
			
			for each(var eventCatObj:Object in remoteEvtCatArray)
			{
				var evtCategory:Object = new Object();
				evtCategory.id = eventCatObj.id;
				evtCategory.category = eventCatObj.name;
				evtCategory.modDat = null;
				evtCategory.eventCount = eventCatObj.eventCount;
				evtsCategoryArray.push(evtCategory);
			}
			DataUtil.insertEventCategory(evtsCategoryArray);
		}
		
		protected function onEventsDataRecieved(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			var evtsArray:Array = new Array();
			var remoteEvtArray:Array = jsonArray.sitter.response.getEventList;
			
			for each(var evtObj:Object in remoteEvtArray)
			{
				var evtObject:Object = new Object();
				evtObject.id = evtObj.event_id;
				evtObject.typeId = evtObj.event_type;
				evtObject.name = evtObj.event_title;
				evtObject.eventDt = evtObj.event_date;
				evtObject.location = evtObj.event_place;
				evtObject.organiser = evtObj.event_time;
				evtObject.content = evtObj.event_desc;
				evtsArray.push(evtObject);
			}
			DataUtil.insertEvents(evtsArray);
		}
		
		protected function onPositionListDataRecieved(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			
			var jobTitleArray:Array = new Array();
			var remoteJobTitleArray:Array = jsonArray.sitter.response.userRoles;
			
			for each(var jobTitleObj:Object in remoteJobTitleArray)
			{
				var jobTitle:Object = new Object();
				jobTitle.positionGroupId = jobTitleObj.role_id;
				jobTitle.positionGroup = jobTitleObj.role_name;
				jobTitle.modDat = null;
				jobTitleArray.push(jobTitle);
			}
			DataUtil.insertJobPositions(jobTitleArray);
		}
		
		protected function onTeamListDataRecieved(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			
			var employeeArray:Array = new Array();
			var siteEmpArray:Array = jsonArray.sitter.response.siteEmployees;
			
			DataUtil.checkAndInsertEmployees(siteEmpArray);
		}
		
		protected function onError(event:Event):void
		{
			trace(event);
		}
		
		protected function onBusinessDataReceived(event:Event):void
		{
			var jsonArray:Object = JSON.parse(event.target.data);
			var id:String = jsonArray.sitter.response.getInfos[0].info_id;
			var headingText:String = jsonArray.sitter.response.getInfos[0].page_title;
			var messageText:String = jsonArray.sitter.response.getInfos[0].page_content;
			var modifiedOn:String = jsonArray.sitter.response.getInfos[0].modified_on;
			
			DataUtil.insertBusinessData(id, headingText, messageText, modifiedOn);
		}
		
		private var emailText:String;
		public function postEmailData(emailTxt:String):void
		{
			emailText = emailTxt;
			var request:URLRequest=new URLRequest();
			request.url="http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=manageStaffInformations&staff_email=" + emailTxt;
			request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onEmailPostSuccess);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		protected function onEmailPostSuccess(event:Event):void
		{
			DataUtil.updateSettings(emailText);
			emailText = null;
		}
	}
}